package com.yunhoo.pay.bean;

import com.ibatis.dao.client.DaoManager;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.asset.util.PayCodeConstants;
import com.yunhoo.base.bean.FinancialProjectBean;
import com.yunhoo.base.db.dao.FinancialProjectDao;
import com.yunhoo.base.db.dao.PersonDao;
import com.yunhoo.base.db.impl.BusinessParameterImpl;
import com.yunhoo.pay.db.impl.AbroadGroupImpl;
import com.yunhoo.base.model.AbroadGroupCurrency;
import com.yunhoo.base.model.Department;
import com.yunhoo.base.model.FinancialProject;
import com.yunhoo.base.model.Partner;
import com.yunhoo.base.model.PayStyle;
import com.yunhoo.base.model.Person;
import com.yunhoo.pay.db.impl.PayApplicationImpl;
import com.yunhoo.pay.db.impl.VoucherPubImpl;
import com.yunhoo.pay.model.Expense;
import com.yunhoo.pay.model.QueryVouchers;
import com.yunhoo.pay.model.VouCheckInfo;
import com.yunhoo.pay.util.BillNumBuilder;
import com.yunhoo.sys.db.config.DaoConfig;
import com.yunhoo.pay.util.BillQueryToolKit;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.MoneyUtil;
import com.yunhoo.sys.util.common.NavigationTool;
import com.yunhoo.sys.util.common.Null;
import com.yunhoo.sys.util.common.PrintToolKit;
import com.yunhoo.sys.util.common.ToolKit;
import com.yunhoo.pay.util.ToolsMethed;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.login.LoginBean;
import com.yunhoo.sys.util.tree.TreeUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFooter;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.richfaces.component.UIDataTable;
import org.richfaces.event.TreeSelectionChangeEvent;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "payApplication")
@SessionScoped
public class PayApplicationBean {

    Expense exp = new Expense();
    private Expense oldExp = new Expense();//修改取消时，恢复对象
    private QueryVouchers qlb = new QueryVouchers();//单据列表查询页面对象
    private int vouIndex = -1;
    private Partner partner = new Partner();
    private LinkedList<Expense> vouchers = new LinkedList<Expense>();
    private List<Expense> billList = new ArrayList<Expense>();
    private DataModel<Expense> billModel = new ListDataModel<Expense>();
    private UIDataTable data = null;
    private List<SelectItem> payStyles = null;
    private List<SelectItem> memo = null;
    private boolean showAble = false;
    private boolean submitOrNot = false;
    private boolean deleteable = false;
    private boolean browStatus = false;//浏览状态可以修改、删除单据
    private boolean editStatus = false;//编辑状态下，只能保存或放弃
    private boolean editDisabled = true;//编辑状态是否禁用
    private boolean printable = false;//单据存在，即可打印
    private boolean addOrUpdate = false;
    private boolean saveOrNot = false;
    private List<SelectItem> payStylelist = null;
    private String payWay;
    private List<SelectItem> currencys = null;
    private static DaoManager daom;
    private static FinancialProjectDao finProjdao;
    private FinancialProject finProj = null;
    private DataModel<FinancialProject> finProjModel = null;
    private String projParam = "";
    private List<SelectItem> perli = null;
    private static PersonDao perDao;

    public PayApplicationBean() {
        daom = DaoConfig.getDaoManager();
        finProjdao = (FinancialProjectDao) daom.getDao(FinancialProjectDao.class);
        perDao = (PersonDao) daom.getDao(PersonDao.class);
        init();
        initPayStyleList();
        //psnPayAppQuery();
        initCurrency();
        initFinProjList();//预算项目列表
    }

    public final void init() {
        payStyles = new ArrayList<SelectItem>();//支付方式列表
        String[] payStrs = new String[]{"现金", "支票", "汇款", "财政授权支付", "财政直接支付", "其他"};
        for (String payStyle : payStrs) {
            payStyles.add(new SelectItem(payStyle, payStyle));
        }
        memo = new ArrayList<SelectItem>();//请款用途列表
        String[] memoStrs = new String[]{"付全款", "预付款", "中期款", "付余款", "附合同", "其他"};
        for (String memoStr : memoStrs) {
            memo.add(new SelectItem(memoStr, memoStr));
        }
        refreshVouchers();
        saveOrNot = true;
        addOrUpdate = false;
        deleteable = true;
        submitOrNot = true;
        showAble = true;
    }

    public final void initCurrency() {
        List<AbroadGroupCurrency> items = AbroadGroupImpl.getAbroadGroupCurrency();
        currencys = new ArrayList<SelectItem>();
        for (AbroadGroupCurrency valueItem : items) {
            currencys.add(new SelectItem(valueItem.getCode().trim(), valueItem.getName().trim()));
        }
    }
    //选择部门

    public void selected(TreeSelectionChangeEvent event) {
        Department dp = (Department) TreeUtil.listener(event);
        exp.getPayDept().setCode(dp.getCode());
        exp.getPayDept().setName(dp.getName());
//        if (!Null.check(exp.getPayDept().getCode())) {
//            queryPerLi();
//        }
    }

    private void queryPerLi() {
        perli = new ArrayList<SelectItem>();
        List<Person> templi = null;
        try {
            Person param = new Person();
            param.setOrgCode(curOrgCode());
            param.setIyear(curIyear());
            param.setDeptCode(exp.getPayDept().getCode());
            //templi = perDao.queryListByDept(param);
            templi = perDao.queryAll();
        } catch (SQLException ex) {
            Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (templi != null) {
            for (Person p : templi) {
                perli.add(new SelectItem(p.getCpsn_number().trim(), p.getCpsn_name().trim()));
            }
        }
    }

    private void initFinProjList() {
        if (finProjModel == null) {
            finProjModel = new ListDataModel<FinancialProject>();
        }
        try {
            List<FinancialProject> finli = finProjdao.queryAll(new FinancialProject(true));
            finProjModel.setWrappedData(finli);
        } catch (SQLException ex) {
            Logger.getLogger(FinancialProjectBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //选择预算项目

    public void projSelected() {
        finProj = finProjModel.getRowData();
        exp.getProject().setFinCode(finProj.getFinCode());
        exp.getProject().setFinName(finProj.getFinName());
        exp.getProject().setCostType(finProj.getCostType());
    }

    public final void initPayStyleList() {
        payStylelist = new ArrayList();
        List<PayStyle> Styleli = AbroadGroupImpl.getPaystyleliNew("4");
        for (PayStyle d : Styleli) {
            payStylelist.add(new SelectItem(d.getCode().trim(), d.getName().trim()));
        }
    }

    public void payStyleChange(ValueChangeEvent value) {
        String code = (String) value.getNewValue();
        String name = AbroadGroupImpl.getPayStyleName(code);
        exp.getPayStyle().setCode(code);
        exp.getPayStyle().setName(name);
    }

    public void add() {
        this.setExp(new Expense());
        this.setPartner(new Partner());
        //exp.setNumber(ToolsMethed.getPayApplicationNumber());
        exp.getUser().setCpsn_name(curPerName().trim());
        exp.getDept().setName(curDeptName().trim());
        exp.getPayDept().setCode(curDeptCode().trim());
        exp.getPayDept().setName(curDeptName().trim());
        exp.getFeePer().setCpsn_number(curPerNum().trim());
        queryPerLi();
        editDisabled = false;
        editStatus = true;
        browStatus = false;
        addOrUpdate = true;
        saveOrNot = false;
    }

    public void update() {
        if (!Null.check(exp.getNumber())) {
            vouIndex = vouchers.indexOf(exp);
            addOrUpdate = false;
            submitOrNot = false;
            editDisabled = false;
            editStatus = true;
            browStatus = false;
            saveOrNot = false;
        } else {
            ToolKit.msgs_info("没有单据");
        }
    }

    public void rescindApp() {
        PayApplicationImpl.rescindApp(exp.getNumber().trim());
        saveOrNot = true;
        addOrUpdate = false;
        exp.setNumberStatus("1");
        browStatus = true;
    }

    public void refreshVouchers() {
        vouchers = new LinkedList();
        Map map = new HashMap();
        map.put("cpsn_number", curPerNum().trim());
        map.put("orgCode", curOrgCode().trim());
        vouchers.addAll(PayApplicationImpl.appListByPerson(map));
    }

    public void initNewStatus() {
        editStatus = false;
        addOrUpdate = false;
        deleteable = true;
        showAble = false;
        submitOrNot = true;
        browStatus = false;
        initVoucher();
        refreshVouchers();
    }

    public void initVoucher() {
        initNewvoucher();
        refreshVouchers();
    }

    public void initNewvoucher() {
        this.setExp(new Expense());
        this.setPartner(new Partner());
    }

    //保存检查
    public void checkSave() {
        int flag = 0;
        projParam = "1";
        String paramValue = BusinessParameterImpl.queryParamValue(projParam.trim());//1、必填 0、可不必须填写
        if (exp.getNumDate() == null) {
            ToolKit.msgs_err("请选择单据日期!");
        } else if (Null.check(partner.getSimplename())) {
            ToolKit.msgs_err("收款单位不能为空!");
        } else if (Null.check(partner.getBank())) {
            ToolKit.msgs_err("开户行不能为空!");
        } else if (Null.check(partner.getBankAccount())) {
            ToolKit.msgs_err("账号不能为空!");
        } else if (Null.check(exp.getAffair())) {
            ToolKit.msgs_err("用途不能为空!");
        } else if (exp.getTotal() == null || exp.getTotal() == 0d) {
            ToolKit.msgs_err("金额不能为空!");
        } else if (Null.check(exp.getProject().getFinCode()) && paramValue.equals("1")) {
            ToolKit.msgs_err("预算项目不能为空!");
        } else if (Null.check(exp.getFeePer().getCpsn_number())) {
            ToolKit.msgs_err("请选择申请人!");
        } else if (Null.check(exp.getPayStyle().getCode())) {
            ToolKit.msgs_err("请选择支付方式!");
        } else if (Null.check(exp.getCurrency().getCode())) {
            ToolKit.msgs_err("请选择币种!");
        } else if (Null.check(exp.getAffixAmount())) {
            ToolKit.msgs_err("请填写附件张数!");
        } else {
            SqlMapClient client = IbatisTemplate.getSqlMapClient();
            try {
                client.startTransaction();

                save(client);

                client.commitTransaction();

                ToolKit.msgs_info("请点击【提交】按钮提交单据并打印本单据!");
                flag = 1;

            } catch (SQLException e) {
                flag = -1;
            } finally {
                try {
                    client.endTransaction();
                } catch (SQLException ex) {
                    Logger.getLogger(PayApplicationBean.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }


        if (flag == 1) {

            refreshVouchers();//重载单据集合
            addOrUpdate = false;
            editDisabled = true;
            editStatus = false;
            browStatus = true;
            saveOrNot = true;

        } else if (flag == -1) {
            ToolKit.msgs_err("数据库异常，保存失败！");
        }
    }

    public static String getPayApplicationNumber(SqlMapClient client) throws SQLException {
        Object maxCode = client.queryForObject("payApp.maxPayApplicationNumber", LoginParam.getOrg().getCode());
        return String.format("%06d%n", maxCode == null ? 1 : Integer.parseInt(maxCode.toString()) + 1).trim();
    }

    public void save(SqlMapClient client) throws SQLException {
        if (addOrUpdate) {
            //System.out.println("进入保存");
            exp.setVoutype("4");
            //String id = getPayApplicationNumber(client);
            String id = BillNumBuilder.buildNum(client);
            exp.setNumber(id);
            exp.getUser().setCpsn_number(curPerNum().trim());
            exp.getDept().setCode(curDeptCode().trim());
            PayApplicationImpl.insertAppBillVoucher(client, exp);
            partner.setId(id);//伙伴信息即收款单位信息，编号与单据号一致
            partner.setOrgCode(curOrgCode().trim());
            PayApplicationImpl.insertPartner(client, partner);
        }
        if (!addOrUpdate) {
            vouIndex = vouchers.indexOf(exp);
            exp.setNumberStatus("1");
            PayApplicationImpl.updateAppBill(client, exp);
            PayApplicationImpl.updatePartner(client, partner);
        }
    }

    //提交
    public void submitApp() {
        PayApplicationImpl.submitApp(exp.getNumber().trim());
        //删除历史审核信息
        VouCheckInfo info = new VouCheckInfo();
        info.setBillNum(exp.getNumber());
        info.setVouType(exp.getVoutype());
        VoucherPubImpl.deleteCheckInfo(info);

        exp.setNumberStatus("0");
        browStatus = false;
        addOrUpdate = true;
        ToolKit.msgs_info("单据已提交成功，等待审核!");
    }

    public void cancel() {
        exp = new Expense();
        initNewStatus();
        FacesContext.getCurrentInstance().renderResponse();
    }

    public void queryCancel() {
        qlb = new QueryVouchers();
        Map map = BillQueryToolKit.billQueryParamMap(qlb);
        map.put("cpsn_number", curPerNum().trim());
        billList = PayApplicationImpl.appBillListForPerson(map);
    }

    public void getFirst() {
        if (vouchers.size() > 0) {
            this.setExp(vouchers.getFirst());
            queryPerLi();
            setVoucherPartner();
            browStatus = true;
        } else {
            ToolKit.msgs_info("您尚未录入单据");
        }
    }

    public void previous() {
        int index = vouchers.indexOf(exp);
        if (index > 0) {
            this.setExp(vouchers.get(index - 1));
            queryPerLi();
            setVoucherPartner();
            browStatus = true;
        } else {
            ToolKit.msgs_info("没有了");
        }
    }

    public void next() {
        if (vouchers != null && vouchers.size() > 0) {
            int index = vouchers.indexOf(exp);
            if (index < vouchers.size() - 1) {
                this.setExp(vouchers.get(index + 1));
                queryPerLi();
                setVoucherPartner();
                browStatus = true;
            }
        } else {
            ToolKit.msgs_info("没有了");
        }
    }

    public void getLast() {
        if (vouchers != null && vouchers.size() > 0) {
            this.setExp(vouchers.getLast());
            setVoucherPartner();
            queryPerLi();
            browStatus = true;
        } else {
            ToolKit.msgs_info("您尚未录入单据");
        }
    }
    //选择预算项目

    public void selectProjForQuery() {
        FinancialProject fProj = finProjModel.getRowData();
        qlb.getProject().setFinCode(fProj.getFinCode());
        qlb.getProject().setFinName(fProj.getFinName());
    }

    public void psnPayAppQuery() {
        Map map = BillQueryToolKit.billQueryParamMap(qlb);
        map.put("cpsn_number", curPerNum().trim());
        map.put("orgCode", curOrgCode().trim());
        billList = PayApplicationImpl.appBillListForPerson(map);
        billModel.setWrappedData(billList);
    }

    //申请单列表中选择并跳转
    public void showBill() {
        exp = (Expense) billModel.getRowData();
        queryPerLi();
        if (exp.getNumberStatus().trim().equalsIgnoreCase("0")) {
            exp.setCheckaccount(exp.getTotal());
        }
        setVoucherPartner();
        billModel.setWrappedData(new ArrayList<Expense>());

        addOrUpdate = false;
        editStatus = false;

        NavigationTool.getSelectedUrl("/page/pay/business/person/payApplication.xhtml");
    }

    public void clearQueryResult(ActionEvent e) {
        billModel.setWrappedData(new ArrayList<Expense>());
    }

    public void setVoucherPartner() {
        Partner part = PayApplicationImpl.partnerById(exp.getNumber().trim());
        //System.out.println(exp.getNumber().trim());
        if (part != null) {
            this.setPartner(part);
        } else {
            this.setPartner(new Partner());
        }
        if ("1".equals(exp.getNumberStatus().trim())) {
            submitOrNot = false;
        } else {
            submitOrNot = true;
        }
    }

    public void delete() {
        PayApplicationImpl.delApp(exp.getNumber().trim());
        initNewStatus();
        refreshVouchers();
    }

    //打印
    public void process(OutputStream outStream, Object data) throws Exception {
        printBill();
    }

    public void printBill() {
        try {
            PrintToolKit.print(reportParameter(), "/page/pay/print/payApplication.jasper");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private Map reportParameter() {
        Map map = new HashMap();
        //System.out.println("Currency:" + exp.getCurrency().getCode());
        if (exp.getCurrency().getCode().trim().equals("RMB")) {
            map.put("numDate", exp.getNumDate());
            map.put("number", exp.getNumber());
            map.put("deptname", exp.getDept().getName());
            map.put("cpsn_name", exp.getUser().getCpsn_name());
            map.put("costType", exp.getCostType());
            map.put("partner", this.getPartner().getSimplename());//收款单位
            map.put("paycode", exp.getPaycode());
            map.put("bank", this.getPartner().getBank());
            map.put("projname", exp.getProject().getFinName()); //项目内容
            map.put("bankAccount", this.getPartner().getBankAccount());
            map.put("projCode", exp.getCustomProjCode());//自定义项目编码
            map.put("affair", exp.getAffair()); //用途
            map.put("money", MoneyUtil.toChinese(String.valueOf(Null.convert(exp.getTotal() == null ? 0d : exp.getTotal())))); //人民币大写
            map.put("total", exp.getTotal());//小写
            map.put("name", exp.getPayStyle().getName());//付款方式
            map.put("memo", exp.getMemo());
            map.put("affixAmount", exp.getAffixAmount());//附单据张数
            map.put("moneyc", ""); //大写
            map.put("sign", "");
            map.put("totalc", null);
        } else if (exp.getCurrency().getCode().trim().equals("ECU")) {
            map.put("numDate", exp.getNumDate());
            map.put("number", exp.getNumber());
            map.put("deptname", exp.getDept().getName());
            map.put("cpsn_name", exp.getUser().getCpsn_name());
            map.put("costType", exp.getCostType());
            map.put("partner", this.getPartner().getSimplename());//收款单位
            map.put("paycode", exp.getPaycode());
            map.put("bank", this.getPartner().getBank());
            map.put("projname", exp.getProject().getFinName()); //项目内容
            map.put("bankAccount", this.getPartner().getBankAccount());
            map.put("projCode", exp.getProjCode());
            map.put("affair", exp.getAffair()); //用途
            map.put("money", ""); //人民币大写
            map.put("total", null);//小写
            map.put("name", exp.getPayStyle().getName());//付款方式
            map.put("memo", exp.getMemo());
            map.put("affixAmount", exp.getAffixAmount());//附单据张数
            map.put("moneyc", MoneyUtil.toChinese(String.valueOf(Null.convert(exp.getTotal() == null ? 0d : exp.getTotal())))); //大写
            map.put("sign", "€");
            map.put("totalc", exp.getTotal());
        } else {
            //if (exp.getCurrency().getCode().trim() != "ECU")
            map.put("numDate", exp.getNumDate());
            map.put("number", exp.getNumber());
            map.put("deptname", exp.getDept().getName());
            map.put("cpsn_name", exp.getUser().getCpsn_name());
            map.put("costType", exp.getCostType());
            map.put("partner", this.getPartner().getSimplename());//收款单位
            map.put("paycode", exp.getPaycode());
            map.put("bank", this.getPartner().getBank());
            map.put("projname", exp.getProject().getFinName()); //项目内容
            map.put("bankAccount", this.getPartner().getBankAccount());
            map.put("projCode", exp.getProjCode());
            map.put("affair", exp.getAffair()); //用途
            map.put("money", ""); //人民币大写
            map.put("total", null);//小写
            map.put("name", exp.getPayStyle().getName());//付款方式
            map.put("memo", exp.getMemo());
            map.put("affixAmount", exp.getAffixAmount());//附单据张数
            map.put("moneyc", MoneyUtil.toChinese(String.valueOf(Null.convert(exp.getTotal() == null ? 0d : exp.getTotal())))); //大写
            map.put("sign", exp.getCurrency().getSign().trim());
            map.put("totalc", exp.getTotal());
        }
        map.put("paystyle", exp.getPayStyle().getName());
        map.put("isBaseCost", isBaseCost());//是基本支出
        return map;
    }

    private String isBaseCost() {
        if (exp.getProject() == null || Null.check(exp.getProject().getFinCode())) return "";
        else if (exp.getProject().getCostType() == 1) return "0";
        else if (exp.getProject().getCostType() == 0) return "1";
        else return "";
    }

    public void exportExcel() {
        HSSFWorkbook demoWorkBook = new HSSFWorkbook();
        HSSFSheet demoSheet = demoWorkBook.createSheet("个人单据付款申请单列表");
        String[] excelColumns = initTabeleHeader();
        List<Expense> rows = billList;

        HSSFHeader header = demoSheet.getHeader();
        header.setCenter("个人单据付款申请单列表");
        HSSFRow headerRow = demoSheet.createRow(0);
        for (int i = 0; i < excelColumns.length; i++) {
            HSSFCell headerCell = headerRow.createCell((short) i);
            //headerCell.setEncoding(HSSFCell.ENCODING_UTF_16);
            headerCell.setCellValue(excelColumns[i]);
        }
        //填充数据
        int rowIndex = 1;
        for (Expense row : rows) {
            createTableRow(demoWorkBook, demoSheet, row, rowIndex);
            rowIndex++;
        }
        exportExcel(demoWorkBook, demoSheet);

    }
    //初始化表头信息

    public String[] initTabeleHeader() {
        String[] excelColumns = {"单据编号", "单据状态", "单据日期", "借款事由", "借款金额", "预算项目", "备注", "核定金额",
            "审核日期", "审核人", "支付金额", "支付方式", "支付日期", "出纳"};
        return excelColumns;
    }

    public static void createTableRow(HSSFWorkbook demoWorkBook, HSSFSheet demoSheet, Expense eit, int rowIndex) {
        //创建第rowIndex行
        CreationHelper createHelper = demoWorkBook.getCreationHelper();
        HSSFCellStyle dstyle = demoWorkBook.createCellStyle();
        dstyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-mm-dd"));//格式

        HSSFCell cell = null;
        HSSFRow row = demoSheet.createRow(rowIndex);
        cell = row.createCell(0);
        cell.setCellValue(PayCodeConstants.convertString(eit.getNumber()));
        cell = row.createCell(1);
        cell.setCellValue(PayCodeConstants.convertString(PayCodeConstants.functionNumberStatus().get(eit.getNumberStatus())));
        cell = row.createCell(2);
        cell.setCellValue(PayCodeConstants.convertDate(eit.getNumDate()));
        cell.setCellStyle(dstyle);
        cell = row.createCell(3);
        cell.setCellValue(PayCodeConstants.convertString(eit.getAffair()));
        cell = row.createCell(4);
        cell.setCellValue(PayCodeConstants.convertDouble(eit.getTotal()));
        cell = row.createCell(5);
        cell.setCellValue(PayCodeConstants.convertString(eit.getProject().getFinName()));
        cell = row.createCell(6);
        cell.setCellValue(PayCodeConstants.convertString(eit.getMemo()));
        cell = row.createCell(7);
        cell.setCellValue(PayCodeConstants.convertDouble(eit.getCheckaccount()));
        cell = row.createCell(8);
        if (eit.getAuditDate() == null) {
            cell.setCellValue("");
        } else {
            cell.setCellValue(PayCodeConstants.convertDate(eit.getAuditDate()));
            cell.setCellStyle(dstyle);
        }
        cell = row.createCell(9);
        cell.setCellValue(PayCodeConstants.convertString(eit.getAuditor()));
        cell = row.createCell(10);
        cell.setCellValue(PayCodeConstants.convertDouble(eit.getPayaccount()));
        cell = row.createCell(11);
        cell.setCellValue(PayCodeConstants.convertString(eit.getPayStyle().getCode()));
        cell = row.createCell(12);
        if (eit.getPayDate() == null) {
            cell.setCellValue("");
        } else {
            cell.setCellValue(PayCodeConstants.convertDate(eit.getPayDate()));
            cell.setCellStyle(dstyle);
        }
        cell = row.createCell(13);
        cell.setCellValue(PayCodeConstants.convertString(eit.getPayer()));

    }

    public static void exportExcel(HSSFWorkbook demoWorkBook, HSSFSheet demoSheet) {
        try {
            String strfileName = "book.xls";
            FileOutputStream fos = null;
            //导出
            demoSheet.setGridsPrinted(true);//demoSheet
            HSSFFooter footer = demoSheet.getFooter();
            footer.setRight("Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages());
            // 获得JSF上下文环境
            FacesContext context = FacesContext.getCurrentInstance();
            // 获得ServletContext对象
            ServletContext servletContext = (ServletContext) context.getExternalContext().getContext();
            // 取得文件的绝对路径
            String excelName = servletContext.getRealPath("/upload") + "/" + strfileName;//strfileName
            File exportFile = new File(excelName);
            fos = new FileOutputStream(exportFile);//fos
            demoWorkBook.write(fos);//demoWorkBook
            fos.close();

            FacesContext ctx = FacesContext.getCurrentInstance();
            ctx.responseComplete();
            HttpServletResponse httpServletResponse = (HttpServletResponse) ctx.getExternalContext().getResponse();
            ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
            httpServletResponse.setHeader("Content-disposition", "attachment; filename=" + strfileName);//strfileName
            httpServletResponse.setContentLength((int) exportFile.length());//exportFile
            httpServletResponse.setContentType("application/x-download");
            //httpServletResponse.setContentType("application/vnd.ms-excel");
            byte[] b = new byte[1024];
            int i = 0;
            FileInputStream fis = new java.io.FileInputStream(exportFile);
            while ((i = fis.read(b)) > 0) {
                servletOutputStream.write(b, 0, i);
            }
            servletOutputStream.flush();
            ctx.responseComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Expense getExp() {
        return exp;
    }

    public void setExp(Expense exp) {
        this.exp = exp;
    }

    public List<SelectItem> getMemo() {
        return memo;
    }

    public void setMemo(List<SelectItem> memo) {
        this.memo = memo;
    }

    public Partner getPartner() {
        return partner;
    }

    public void setPartner(Partner partner) {
        this.partner = partner;
    }

    public List<SelectItem> getPayStyles() {
        return payStyles;
    }

    public void setPayStyles(List<SelectItem> payStyles) {
        this.payStyles = payStyles;
    }

    public boolean isShowAble() {
        return showAble;
    }

    public void setShowAble(boolean showAble) {
        this.showAble = showAble;
    }

    public boolean isSubmitOrNot() {
        return submitOrNot;
    }

    public void setSubmitOrNot(boolean submitOrNot) {
        this.submitOrNot = submitOrNot;
    }

    public boolean isDeleteable() {
        return deleteable;
    }

    public void setDeleteable(boolean deleteable) {
        this.deleteable = deleteable;
    }

    public boolean isAddOrUpdate() {
        return addOrUpdate;
    }

    public void setAddOrUpdate(boolean addOrUpdate) {
        this.addOrUpdate = addOrUpdate;
    }

    public boolean isBrowStatus() {
        return browStatus;
    }

    public void setBrowStatus(boolean browStatus) {
        this.browStatus = browStatus;
    }

    public boolean isEditStatus() {
        return editStatus;
    }

    public void setEditStatus(boolean editStatus) {
        this.editStatus = editStatus;
    }

    public boolean isEditDisabled() {
        return editDisabled;
    }

    public void setEditDisabled(boolean editDisabled) {
        this.editDisabled = editDisabled;
    }

    public QueryVouchers getQlb() {
        return qlb;
    }

    public void setQlb(QueryVouchers qlb) {
        this.qlb = qlb;
    }

    public List<Expense> getBillList() {
        return billList;
    }

    public void setBillList(List<Expense> billList) {
        this.billList = billList;
    }

    public List<SelectItem> getPayStylelist() {
        return payStylelist;
    }

    public void setPayStylelist(List<SelectItem> payStylelist) {
        this.payStylelist = payStylelist;
    }

    public DataModel<Expense> getBillModel() {
        return billModel;
    }

    public void setBillModel(DataModel<Expense> billModel) {
        this.billModel = billModel;
    }

    public List<SelectItem> getCurrencys() {
        return currencys;
    }

    public void setCurrencys(List<SelectItem> currencys) {
        this.currencys = currencys;
    }

    public UIDataTable getData() {
        return data;
    }

    public void setData(UIDataTable data) {
        this.data = data;
    }

    public Expense getOldExp() {
        return oldExp;
    }

    public void setOldExp(Expense oldExp) {
        this.oldExp = oldExp;
    }

    public String getPayWay() {
        return payWay;
    }

    public void setPayWay(String payWay) {
        this.payWay = payWay;
    }

    public boolean isPrintable() {
        return printable;
    }

    public void setPrintable(boolean printable) {
        this.printable = printable;
    }

    public int getVouIndex() {
        return vouIndex;
    }

    public void setVouIndex(int vouIndex) {
        this.vouIndex = vouIndex;
    }

    public LinkedList<Expense> getVouchers() {
        return vouchers;
    }

    public void setVouchers(LinkedList<Expense> vouchers) {
        this.vouchers = vouchers;
    }
    //

    private String curPerNum() {
        return LoginParam.getPer().getCpsn_number();
    }

    private String curPerName() {
        return LoginParam.getPer().getCpsn_name();
    }

    private String curDeptCode() {
        return LoginParam.getDept().getCode();
    }

    private String curDeptName() {
        return LoginParam.getDept().getName();
    }

    private Date curLoginDate() {
        return LoginParam.getLoginDate();
    }

    private Integer curIyear() {
        return LoginParam.getIyear();
    }

    private String curOrgCode() {
        return LoginParam.getOrg().getCode();
    }

    public FinancialProject getFinProj() {
        return finProj;
    }

    public void setFinProj(FinancialProject finProj) {
        this.finProj = finProj;
    }

    public DataModel<FinancialProject> getFinProjModel() {
        return finProjModel;
    }

    public void setFinProjModel(DataModel<FinancialProject> finProjModel) {
        this.finProjModel = finProjModel;
    }

    public boolean isSaveOrNot() {
        return saveOrNot;
    }

    public void setSaveOrNot(boolean saveOrNot) {
        this.saveOrNot = saveOrNot;
    }

    public String getProjParam() {
        return projParam;
    }

    public void setProjParam(String projParam) {
        this.projParam = projParam;
    }

    public List<SelectItem> getPerli() {
        if (perli == null) queryPerLi();
        return perli;
    }

    public void setPerli(List<SelectItem> perli) {
        this.perli = perli;
    }
}
